ihelpuoo.namespace("ihelpuoo.widget.tree");

ihelpuoo.widget.tree.TreeView = function(gasket
										,name
										,ptnName
										,nodePtnName)
{
	this.gasket = gasket;
	this.name = name;
	this.ptnName = (ptnName?ptnName:"eve.pattern.tree.Tree");
	this.view = new ihelpuoo.widget.View(gasket,this.ptnName);
	this.nodePtnName = nodePtnName;
	this.nodes = ihelpuoo.util.map();
	this.rootNodes = new Array();
	ihelpuoo.widget.registerView2(this);
}

ihelpuoo.widget.tree.TreeView.prototype
=
{
	clear: function()
	{
		this.nodes = ihelpuoo.util.map();
		this.rootNodes = new Array();
		this.nodes = ihelpuoo.util.map();
		this.rootNodes = new Array();
		if(this.oArena)
			ihelpuoo.dom.clearAllChildren(this.oArena);	
	}
	
	,
	getNode: function(nodeId)
	{
		return this.nodes.get(nodeId);
	}
	
	,
	setActivedNode: function(node)
	{
		if(this.activedNode)
			this.activedNode.disActived();
		this.activedNode = node;
		this.activedNode.actived();
		if(this.onActiveNode)
			this.onActiveNode(this);
	}
	
	,
	disActive: function()
	{
		if(!this.activedNode) return;

		this.activedNode.disActived();
		var node = this.activedNode;
		this.activedNode = null;
		
		if(this.onDisActiveNode)
			this.onDisActiveNode(this,node);
	}
	
	,
	draw : function(list)
	{
		if(this.view.isNeverBeDrawn())
		{
			this.view.preparePattern();
			this.oArena = this.view.getElement("arena");
			ihelpuoo.widget.addViewEvent(this,this.view.shell,"click",this.onClickShell);
			
		}
		this.clear();
		this.display();
		if(!list || list.length==0) return;
		for(var i=0;i<list.length;i++)
		{
			var node = list[i];
			var nodeView = new ihelpuoo.widget.tree.
												TreeNodeView(
													this.oArena
													,this
													,this.nodePtnName);
			nodeView.draw(node);
			this.rootNodes.push(nodeView);
		}
	}
	
	,
	onClickShell: function()
	{
		ihelpuoo.event.cancelBubble(ihelpuoo.Event(arguments));
		var v = ihelpuoo.widget.getViewByEventSource(this);
		v.disActive();
	}
	
	,
	registerNode: function(nodeView)
	{
		this.nodes.put(nodeView.getId(),nodeView);
	}
	
	,
	degisterNode: function(nodeId)
	{
		this.nodes.remove(nodeId);
	}
	
	// <WEBOO:INHERIT extends="ihelpuoo.widget.View">
	,display: function(){this.view.display();}
	,getElement: function(key){return this.view.getElement(key);}
	,isNeverBeDrawn: function(){return this.view.isNeverBeDrawn();}
	,preparePattern: function(){this.view.preparePattern();}
	,hide: function(){this.view.hide();}
	,set: function(key,value){this.view.set(key,value);}
	,getString: function(key,defValue){return this.view.getString(key,defValue);}
	,getInt: function(key,defValue){return this.view.getInt(key,defValue);}
	,getBoolean: function(key,defValue){return this.view.getBoolean(key,defValue);}
	,addEvent: function(key,eventName,fnHandler,bCapture){this.view.addEvent(key,eventName,fnHandler,bCapture);}
	,removeEvent: function(key,eventName,fnHandler,bCapture){this.view.removeEvent(key,eventName,fnHandler,bCapture);}
	// </WEBOO:INHERIT>
}